//
//  KyTitleView.swift
//  KyPageViewDemo
//
//  Created by fangyukui on 2017/11/21.
//  Copyright © 2017年 fangyukui. All rights reserved.
//

import UIKit

protocol KyTitleViewDelegate:class{
    func titleView(titleView:KyTitleView,targetIdx:NSInteger)
    
}

class KyTitleView: UIView {
    weak var deleagte:KyTitleViewDelegate?
    fileprivate var titles:[String]
    fileprivate var style:KyTitleStyle
    fileprivate lazy var lbs = [UILabel]()
    fileprivate var currentIdx = 0
    fileprivate lazy var scrollView:UIScrollView = {
        let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height))
        scrollView.delegate = self
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        return scrollView
        
    }()
    
    
    
    init(frame: CGRect,titles:[String],style:KyTitleStyle) {
        self.titles = titles
        self.style = style
        super.init(frame: frame)
        backgroundColor = UIColor.random
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension KyTitleView{
    fileprivate func setupUI(){
       addSubview(scrollView)
        
       setupTitleLb()
       setupTitleLbFrame()
        
    }
    private func setupTitleLb(){
        for title in titles {
            let lb = UILabel()
            lb.text = title
            lb.textColor = style.nomalColor
            lb.textAlignment = .center
            lb.isUserInteractionEnabled = true
            lb.font = UIFont.systemFont(ofSize:style.fontSize)
            let tap = UITapGestureRecognizer(target: self, action: #selector(tapClick(tap:)))
            lb.addGestureRecognizer(tap)
            
            scrollView.addSubview(lb)
            lbs.append(lb)
            
        }
        
    }
    private func setupTitleLbFrame(){
        for (i,lb) in lbs.enumerated() {
            var w : CGFloat = 0
            let h : CGFloat = bounds.height
            var x : CGFloat = 0
            let y : CGFloat = 0
            
            if i==0 {
                lb.textColor = style.selectedColor
            }
            
            if !style.isScollEnble {
                w = bounds.width / CGFloat(titles.count)
                x = CGFloat(i) * w
                
                
            }else{ //滚动
                
                w = (lb.text as NSString!).boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: 0), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: style.fontSize)], context: nil).width
                
                if i==0 {
                    x = style.itemMargin / 2.0
                }else{
                    let previous = lbs[i - 1]
                    x = previous.frame.maxX + style.itemMargin
                }
            }
            lb.frame = CGRect(x: x, y: y, width: w, height: h)
         }
        
        scrollView.contentSize.width = style.isScollEnble ? (lbs.last?.frame.maxX )! + style.itemMargin/2.0 : (lbs.last?.frame.maxX )!
        scrollView.contentSize.height = bounds.height
    
        
    }
    

}
extension KyTitleView{
    @objc fileprivate func tapClick(tap:UITapGestureRecognizer){
        guard let targetLb =  tap.view as? UILabel else {
            return
        }
        let targetIdx = lbs.index(of: targetLb) ?? 0
        justTitleLb(targetIdx: targetIdx)
        deleagte?.titleView(titleView: self, targetIdx: currentIdx)
        
        
    }
}
extension KyTitleView:KyContentViewDelegate,UIScrollViewDelegate{
    func contentView(contentView: KyContentView, didTargetIdx: Int) {
        justTitleLb(targetIdx: didTargetIdx)
        
    }
    func contentView(contentView: KyContentView, targetIdx: Int, progress: CGFloat) {
        let currentLb = lbs[currentIdx]
        let targerLb = lbs[targetIdx]
        let (rDelt,gDelt,bDelt) = UIColor.colorDelt(nomalColor: style.nomalColor, selectedColor: style.selectedColor)
        
        currentLb.textColor = UIColor(r: style.nomalColor.getRGB().0 + rDelt * progress, g: style.nomalColor.getRGB().1 + gDelt * progress, b: style.nomalColor.getRGB().2 + bDelt * progress)
        
        targerLb.textColor = UIColor(r: style.selectedColor.getRGB().0 - rDelt * progress, g: style.selectedColor.getRGB().1 - gDelt * progress, b: style.selectedColor.getRGB().2 - bDelt * progress)
        
        
        
    }
    fileprivate func justTitleLb(targetIdx:Int){

        let targetLb = self.lbs[targetIdx]
        
        lbs[currentIdx].textColor = style.nomalColor
        
        targetLb.textColor = style.selectedColor
        
        currentIdx = targetIdx
        
        var offset = targetLb.center.x - bounds.width/2.0
        if offset < 0 {
            offset = 0
        }
        if offset > scrollView.contentSize.width - bounds.width {
            offset =  scrollView.contentSize.width - bounds.width
        }
        
        scrollView.setContentOffset(CGPoint(x: offset, y: 0), animated: true)
        
    }
    
    
}
